﻿function onAddBtn() {
    var $ = layui.jquery;
    var layer = layui.layer; // 添加layui弹出层模块
    var laydate = layui.laydate;
    //渲染
    laydate.render({
        elem: '#storageDate'
    });

    layer.open({
        type: 1,
        title: "新增网格类型",
        area: ['40%', '40%'],
        shade: 0,
        shadeClose: true,
        maxmin: true,
        anim: 0,
        content: $("#add-main"),
    });
}
layui.use(['table', 'laypage', 'layer', 'form'], function () {
    var table = layui.table;
    var laypage = layui.laypage;
    var $ = layui.jquery;
    var form = layui.form;
    var layer = layui.layer; // 添加layui弹出层模块

    var checkedIds = []; // 存储选中节点的 id
    var checkedIdsAll = [];

    var size = 15; // 每页显示条数
    //获取列表  显示
    function getListData(pindex) {
        //获取查询条件
        var gridname = $('#gridname').val();
        var gridclass = $('#gridclass').val();
        //ajax后台交互
        $.ajax({
            url: 'http://8.140.245.229:8000/GridRed/api/MeshType/Handle',
            type: 'get',
            dataType: 'json',
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            //参数
            data: {
                pageIndex: pindex,
                pageSize: size,
                gridname: gridname,
                gridclass: gridclass
            },
            success: function (res) {
                //渲染表格数据
                layui.use(['table', 'laypage'], function () {
                    var table = layui.table;
                    var laypage = layui.laypage;

                    table.render({
                        //挂载的表格id
                        elem: '#ID-table-demo-setRowChecked',
                        cols: [[
                            { type: 'checkbox', unresize: true, align: 'center', fixed: 'center', },
                            { type: 'numbers', title: '序号', align: 'center', fixed: 'center', },
                            { field: 'gridName', title: '网格类型名称', align: 'center', fixed: 'center', },
                            {
                                field: 'gridClass', title: '网格类别', fixed: 'center', align: 'center'
                            },

                            { title: '操作', templet: '#barDemo', fixed: 'right', unresize: true, align: 'center', fixed: 'center', },

                        ]],
                        toolbar: '#demo-toolbar-setRowChecked',
                        initSort: { // 设置初始排序
                            field: 'id', // 字段名
                            type: 'desc' // 倒序
                        },
                        data: res.list
                    });

                    //分页
                    laypage.render({
                        elem: 'pagesbox', // 元素 id
                        count: res.totalcount, // 数据总数
                        limit: size,
                        curr: pindex,
                        layout: ['count', 'prev', 'page', 'next', 'skip'], // 功能布局
                    });

                });

            }
        });
    }
    //点击事件触发显示功能   查询
    $('#searchBtn').on('click', function () {
        getListData(1);
    });
    //绑定网格类型
    $(document).ready(function () {
        $.ajax({
            url: 'http://8.140.245.229:8000/GridRed/api/MeshType/GridClassList',
            type: 'get',
            dataType: 'json',
            success: function (res) {
                data = res; // 直接使用返回的数据
                var select = $('#value');
                $.each(data, function (index, item) {
                    // 使用正确的字段添加选项到下拉框中
                    var option = $('<option>').val(item.name).text(item.name);
                    select.append(option);
                });
                // 手动触发 layui 组件的渲染
                layui.form.render('select');
            },
            error: function () {
                // 处理 AJAX 请求失败的情况
                console.error('Failed to fetch data for dropdown.');
            }
        });
    });
    var obj = {};
    // 提交事件
    form.on('submit(demo2)', function (data) {
        var field = data.field;
        var index = parent.layer.getFrameIndex(window.name);


        obj.gridName = field.gridName;
        obj.gridClass = field.gridClass;
        obj.professionalGrid = field.professionalGrid;
        $.ajax({
            url: "http://8.140.245.229:8000/Grid/api/MeshType/MeshTypeAdd",
            type: 'post',
            data: JSON.stringify(obj),
            contentType: "application/json",
            success: res => {
                if (res > 0) {
                    layer.alert('添加成功');
                    parent.layer.close(index);//关闭当前弹窗页面
                    location.href = "/GridManagement/GridTypeManagement";
                }
                else if (res == -1) {
                    layer.msg('网格类型名称不能重复：');
                } else {
                    layer.msg('添加失败：');
                }
            },
            error: function () {
                // 请求失败的处理
                layer.msg('请求失败，请重试');
            }
        });
        // 阻止表单默认提交
        return false;
    });
    // 单删除
    function deleteData(id) {
        $.ajax({
            url: `http://8.140.245.229:8000/api/Grid/MeshType/MeshTypeDelete?id=${id}`,
            type: 'delete',
            contentType: "application/json",
            success: function (res) {
                if (res > 0) {
                    layer.msg('删除成功');
                    getListData(1);
                } else {
                    layer.msg('删除失败');
                    getListData(1);
                }
            },
            error: function () {
                layer.msg('删除请求失败');
            }
        });

    }

    // 监听工具条，实现删除功能
    table.on('tool(ID-table-demo-setRowChecked)', function (obj) {
        var data = obj.data;
        if (obj.event == 'delete') {
            layer.confirm('确定删除吗？', function (index) {
                deleteData(data.id); // 传递正确的数据项ID
                layer.close(data);
            });
        }
        if (obj.event == 'edit') {

        }
    });
    $(function () {
        // 初始化加载数据
        getListData(1);
    })
});
